﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FixtureManageSystem.Commands;
using FixtureManageSystem.Models;
using System.Windows.Input;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace FixtureManageSystem.ViewModels
{

    class LedgerViewMode : INotifyPropertyChanged
    {
        /// <summary>
        /// 获取统计信息model的命令
        /// </summary>
        private ICommand getLedgerCommand;
        public ICommand GetLedgerCommand
        {
            get { return getLedgerCommand; }
            set { getLedgerCommand = value; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public LedgerService legderService = new LedgerService();
        public FixtureService fixtureService = new FixtureService();

        private Ledger ledgerModel;
        public Ledger LedgerModel
        {
            get { return ledgerModel; }
            set { if(value != ledgerModel)
                {
                    ledgerModel = value;
                    NotifyPropertyChanged();
                } }
        }

        /// <summary>
        /// 数据服务字段
        /// </summary>
        public LedgerService ledgerService = new LedgerService();

        public async void GetLedger()
        {
            try
            {
                SearchList.Clear();
                isSearchMode = false;
                InitFixtureList();
                LedgerModel =  await ledgerService.GetLedger();
                Message = "刷新成功！";
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }

        private void SearchFixture()
        {
            Fixture searchItem = new Fixture();
            searchItem.Name = Name;
            searchItem.MaterialType = MaterialType;
            searchItem.BarCode = Barcode;

            try
            {
                isSearchMode = true;
                SearchList = new ObservableCollection<Fixture>(fixtureService.Search(searchItem));
                InitFixtureList();
            }

            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }
        public LedgerViewMode()
        {
            GetLedgerCommand = new DelegateCommand(GetLedger);
            BtnFirstCommand = new DelegateCommand(BtnFitst);
            BtnNextCommand = new DelegateCommand(BtnNext);
            BtnLastCommand = new DelegateCommand(BtnLast);
            BtnPrevCommand = new DelegateCommand(BtnPrev);
            SearchCommand = new DelegateCommand(SearchFixture);
            GetLedger();

        }

        public ObservableCollection<Fixture> SearchList { get; set; } = new ObservableCollection<Fixture>();

        private ObservableCollection<Fixture> fixtureList;
        public ObservableCollection<Fixture> FixtureList
        {
            get
            {
                return fixtureList;
            }
            set
            {
                fixtureList = value;
                NotifyPropertyChanged();
            }
        }

        // 显示第一页
        public ICommand BtnFirstCommand { get; set; }
        // 显示前一页
        public ICommand BtnPrevCommand { get; set; }
        // 显示后一页
        public ICommand BtnNextCommand { get; set; }
        // 显示最后一页
        public ICommand BtnLastCommand { get; set; }

        public ICommand SearchCommand { get; set; }

        // 总页数
        private int totalCount;
        public int TotalCount { get { return totalCount; } set { totalCount = value; NotifyPropertyChanged(); } }

        // 每页大小
        private int pageSize = 10;
        public int PageSize
        {
            get { return pageSize; }
            set
            {
                if (value != pageSize)
                {
                    pageSize = value;
                    Page = 1;
                    InitFixtureList();
                    NotifyPropertyChanged();
                }
            }
        }

        // 每页大小的候选项
        private ObservableCollection<int> pageSizes = new ObservableCollection<int>(new int[] { 10, 20, 30, 50, 100 });
        public ObservableCollection<int> PageSizes { get { return pageSizes; } }

        // 页码数
        private int page;
        public int Page
        {
            get { return page; }
            set
            {
                page = value;
                NotifyPropertyChanged();
            }
        }

        public void BtnFitst()
        {
            try
            {
                Page = 1;
                // 如果是搜索模式
                if (isSearchMode)
                {
                    FixtureList = new ObservableCollection<Fixture>(SearchList.Skip((Page - 1) * PageSize).Take(PageSize));
                }
                else
                {
                    FixtureList = fixtureService.InquireFixtures(Page, PageSize);
                }
            }

            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }


        /// <summary>
        /// 跳转到搜索结果或者默认试图的前一页
        /// </summary>
        public void BtnPrev()
        {
            if (Page == 1) return;
            Page -= 1;
            try
            {
                // 如果是搜索模式
                if (isSearchMode)
                {

                    FixtureList = new ObservableCollection<Fixture>(SearchList.Skip((Page - 1) * PageSize).Take(PageSize));
                }
                else
                {
                    FixtureList = fixtureService.InquireFixtures(Page, PageSize);
                }
                 
            }

            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }

        /// <summary>
        /// 跳转到搜索结果或者默认试图的前一页
        /// </summary>
        public void BtnNext()
        {
            if (Page == TotalCount) return;
            Page += 1;
            try
            {
                if (isSearchMode)
                {

                    FixtureList = new ObservableCollection<Fixture>(SearchList.Skip((Page - 1) * PageSize).Take(PageSize));
                }
                else
                {
                    FixtureList = fixtureService.InquireFixtures(Page, PageSize);
                }
            }

            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }


        /// <summary>
        /// 跳转到搜索结果或者默认试图的最后一页
        /// </summary>
        public void BtnLast()
        {
            try
            {
                Page = TotalCount;
                if (isSearchMode)
                {

                    FixtureList = new ObservableCollection<Fixture>(SearchList.Skip((Page - 1) * PageSize).Take(PageSize));
                }
                else
                {
                    FixtureList = fixtureService.InquireFixtures(Page, PageSize);
                }
            }

            catch (Exception ex)
            {
                Message = ex.Message;
            }
        }

        /// <summary>
        /// 消息提示
        /// </summary>
        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                message = value;
                if (PropertyChanged != null)
                {
                    System.Windows.MessageBox.Show(message);
                }
            }
        }

        public string Name { get; set; }
        public string Barcode { get; set; }
        public string MaterialType { get; set; }

        // 是否是搜索模式，搜索模式下，所有数据源放在一个SearchList里面，前端界面仅仅显示SearchList的内容
        // 否则显示数据总条数、页数
        private bool isSearchMode = false;

        // 初始化显示的参数
        public void InitFixtureList()
        {
            if (!isSearchMode)
            {
                int totalNum = fixtureService.GetFixturesCount();
                // 计算总页数
                TotalCount = (int)Math.Ceiling((double)totalNum / PageSize);
            }
            else
            {
                TotalCount = (int)Math.Ceiling((double)(SearchList.Count) / PageSize);
            }
            BtnFitst();
        }

    }
}
